﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ImageTools;
using System.IO;
using ImageTools.IO.Jpeg;
using System.Threading;


namespace DBSchemaViewer.FrontEnd.Main.AppUserControls
{
    public partial class ExportWindow : ChildWindow
    {
        private Canvas _theCanvas;
        public event EventHandler MarkSelectedObject;
        public event EventHandler UnMarkSelectedObject;

        /// <summary>
        /// Constructor of the Export Image Windows
        /// It's required the Canvas has to be exported
        /// </summary>
        /// <param name="theCanvasToExport">Canvas to export</param>
        public ExportWindow(Canvas theCanvasToExport)
        {
            InitializeComponent();
            _theCanvas = theCanvasToExport;
        }

        /// <summary>
        /// export the canvas to the selected exit type
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            //Unmark any selected object (table relation) if chkUnmarkSelectedObject is checked
            if (chkUnmarkSelectedObject.IsChecked == true)
            {
                if (UnMarkSelectedObject != null)
                    UnMarkSelectedObject(this, null);
            }

            //Add filter depending of the type selected
            if (rdJpeg.IsChecked == true)
            {
                sfd.Filter = "Image Files (*.jpeg)|*.jpeg";
            }
            else
            {
                sfd.Filter = "Image Files (*.png)|*.png";
            }

            //Show wait message
            Waiting(true);

            //Open ShowDialog window to the user enter file name
            if (sfd.ShowDialog() == true)
            {
                using (Stream stream = sfd.OpenFile())
                {

                    ImageTools.Image myImage = _theCanvas.ToImage();

                    //if JPeg Selected convert to Jpeg
                    if (rdJpeg.IsChecked == true)
                    {
                        JpegEncoder encoder = new JpegEncoder();
                        encoder.Encode(myImage, stream);

                        stream.Close();
                    }
                    //By default the exit format is png
                    else
                    {
                        myImage.WriteToStream(stream);
                    }

                }
            }

            Waiting(false);

            //Mark any previous selected object (table relation) if chkUnmarkSelectedObject is checked
            if (chkUnmarkSelectedObject.IsChecked == true)
            {
                if (MarkSelectedObject != null)
                    MarkSelectedObject(this, null);
            }

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Show Message to inform the user the image is beign generated
        /// </summary>
        /// <param name="value"></param>
        private void Waiting(bool value)
        {
            if (value)
            {
                bProgressBar.Visibility = Visibility.Visible;
            }
            else
            {
                bProgressBar.Visibility = Visibility.Collapsed;
            }
        }
    }
}

